<template>
  <div>
    <div style="margin-bottom: 10px">

    </div>

    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="username"></el-input>
      <el-input style="width: 200px" placeholder="请输入邮箱" suffix-icon="el-icon-message" class="ml-5"
                v-model="email"></el-input>
      <el-input style="width: 200px" placeholder="请输入地址" suffix-icon="el-icon-position" class="ml-5"
                v-model="address"></el-input>
      <el-input style="width: 200px" placeholder="请输入手机号" suffix-icon="el-icon-phone" class="ml-5"
                v-model="phone"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button class="ml-5" type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAddUser">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm
          class="ml-5"
          confirm-button-text='好的'
          cancel-button-text='不用了'
          icon="el-icon-info"
          icon-color="red"
          title="确定删除吗？"
          @confirm="deleteMore"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
      <el-upload
          :limit="1"
          :action="'http://' + serverIp + ':9090/user/import'"
          style="display: inline-block"
          :show-file-list="false"
          accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
          :on-success="handleExcelUploadSuccess"
      >
        <el-button type="primary" class="ml-5">导入 <i class="el-icon-bottom"></i></el-button>
      </el-upload>

      <el-button type="primary" @click="exportFile" class="ml-5">导出 <i class="el-icon-top"></i></el-button>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="headerBg" style="font-size: 15px"
              @selection-change="handleSelectionChangeDeleteMore">
      >
      <el-table-column type="selection"></el-table-column>
      <el-table-column prop="id" label="序号" width="70">
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="130">
      </el-table-column>
      <el-table-column prop="role" label="角色" width="150">
      </el-table-column>
      <el-table-column prop="nickname" label="昵称" width="120">
      </el-table-column>
      <el-table-column prop="phone" label="手机号" width="130">
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="220">
      </el-table-column>
      <el-table-column prop="address" label="地址">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180">
      </el-table-column>
      <el-table-column prop="updateTime" label="修改时间" width="180">
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='好的'
              cancel-button-text='不用了'
              icon="el-icon-info"
              icon-color="red"
              title="确定删除吗？"
              @confirm="handleDelete(scope.row.id)">
            >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i>
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <!--对话框弹出-->
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="120px">
        <el-form-item label="用户名称">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select clearable v-model="form.role" placeholder="请选择角色属性">
            <el-option
                v-for="item in roleList"
                :key="item.name"
                :label="item.name"
                :value="item.flag">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveUser">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {serverIp} from "../../public/config";

export default {
  name: "User",
  data() {
    return {
      serverIp: serverIp,
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      username: '',
      email: '',
      address: '',
      phone: '',
      form: [],//表单数据
      roleList: [],//角色列表
      multipleSelection: [],//多选
      dialogFormVisible: false,//对话框
      headerBg: 'headerBg',
    }
  },
  created() {
    this.load();
  },
  methods: {
    // 加载数据
    load() {
      this.request.get("/user/page", {
        // 参数
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          username: this.username,
          phone: this.phone,
          email: this.email,
          address: this.address,
        }
      }).then(res => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      })

      // 获取角色列表
      this.request.get("/role").then(res => {
        this.roleList = res.data;
      })
    },
    //分页方法实现
    //页码
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    //页码
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    //重置搜索按钮
    reset() {
      this.username = ''
      this.phone = ''
      this.email = ''
      this.address = ''
      this.load()
    },
    //新增用户信息
    handleAddUser() {
      this.form = {
        username: '',
        nickname: '',
        email: '',
        address: '',
        phone: '',
      }
      this.dialogFormVisible = true;
    },
    //保存用户信息
    saveUser() {
      this.request.post("/user/saveOrUpdate", this.form).then(res => {
        if (res.code === '200') {
          this.$message.success('保存成功')
          this.dialogFormVisible = false;
          this.load()
        } else {
          this.$message.error('保存失败')
        }
      })
    },
    //编辑用户信息
    handleEdit(row) {
      this.dialogFormVisible = true;
      this.form = JSON.parse(JSON.stringify(row))  //可以解决编辑未确认就改变的问题

    },
    //删除用户信息
    handleDelete(id) {
      this.request.delete("/user/delOne/" + id).then(res => {
        if (res.code === '200') {
          this.$message.success('删除成功')
          this.load()
        } else {
          this.$message.error('删除失败')
        }
      })
    },
    //批量删除
    handleSelectionChangeDeleteMore(val) {
      console.log(val)
      this.multipleSelection = val
    },
    deleteMore() {
      let ids = this.multipleSelection.map(item => item.id)//[{},{}] => [1,2,3]
      this.request.post("/user/del/batch", ids).then(res => {
        if (res.code === '200') {
          this.$message.success('删除成功')
          this.load()
        } else {
          this.$message.error('删除失败')
        }
      })
    },
    //文件导出操作
    exportFile() {
      window.open(`http://${serverIp}:9090/user/export`)
    },
    //导入文件
    handleExcelUploadSuccess() {
      this.$message.success('导入成功')
      this.load()
    },

  }
}
</script>

<style>
.headerBg {
  background: #eee !important;
}
</style>